package com.itheima.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.pojo.Book;
import com.itheima.pojo.Result;
import com.itheima.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/books")
public class BookController {
    @Autowired
    private BookService bookService;
//查询全部
    @GetMapping
    public Result getAll(){
        return Result.success(bookService.getAll());
    }
//新增
    @PostMapping
    public Result save(@RequestBody Book book){
        bookService.save(book);
        return Result.success();
    }
//跟新修改
    @PutMapping
    public Result update(@RequestBody Book book){
        bookService.update(book);
        return Result.success();
    }
//删除
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        bookService.delete(id);
        return Result.success();
    }
//按id查询数据
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        return Result.success(bookService.getById(id));
    }
//分页查询
    @GetMapping("{currentPage}/{pageSize}")
    public Result getPage(@PathVariable int currentPage,@PathVariable int pageSize){
        return Result.success(bookService.getPage(currentPage,pageSize));
    }

}
